package com.zjg.servlet;

import java.io.IOException;
import java.util.Objects;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zjg.bean.User;


@WebServlet("/loginpage.servlet")
public class InitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		boolean flag=false;
		String param=request.getParameter("param");
		if(Objects.equals("login", param)) {
			
		Cookie[] cookie=request.getCookies();
		if(cookie.length!=0&&null!=cookie) {
		for(int i=0;i<cookie.length;i++) {
			if(cookie[i].getValue().equals("admin")) {
				
				flag=false;
				
			};
		}
		}
		if(flag) {
			request.getRequestDispatcher("views/Order.jsp").forward(request, response);
			}
		else {
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}}
		if(Objects.equals("views", param)) {
			
			HttpSession session=request.getSession();
			User user=(User)session.getAttribute("user");
			request.getRequestDispatcher("views/Order.jsp?id="+user.getId()).forward(request, response);
			
			
			
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
